package com.e2oto.seller.modules.seller.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.GridView;

import com.e2oto.seller.R;
import com.e2oto.seller.common.base.BaseMvpActivity;
import com.e2oto.seller.common.utils.ImageLoader;
import com.e2oto.seller.common.utils.RxBus;
import com.e2oto.seller.common.utils.ToastUtils;
import com.e2oto.seller.modules.seller.adapter.SelectImageAdapter;
import com.e2oto.seller.modules.seller.bean.GoodsInfoBean;
import com.e2oto.seller.modules.seller.bean.RxBusBean;
import com.e2oto.seller.modules.seller.presenter.AddGoodsDetailPresenter;
import com.e2oto.seller.modules.seller.views.AddGoodsDetailView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.iwf.photopicker.PhotoPicker;

/**
 * Created by ZhangLiQiang on 2017/11/23.
 */

/***
 * 只处理了批发商品 批发goodsId = bean.getBatch_goods_id() 零售goodsId = bean.getGoods_Id()
 */
public class AddGoodsDetailActivity extends BaseMvpActivity<AddGoodsDetailView, AddGoodsDetailPresenter> implements AddGoodsDetailView {

    @BindView(R.id.add_goods_detail_gv)
    GridView imageGridView;
    @BindView(R.id.spec_detail_et)
    EditText editText;
    private SelectImageAdapter adapter;
    private String goodsId;

    private GoodsInfoBean bean;

    @Override
    protected AddGoodsDetailPresenter createPresenter() {
        return new AddGoodsDetailPresenter();
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_add_goods_detail;
    }

    @Override
    public void showMessage(String msg) {
        ToastUtils.INSTANCE.showToast(this, msg);
    }

    @Override
    public void addSpecThridCallback() {
        showMessage("添加成功");
        if (bean != null) {
            RxBus.getIntanceBus().post(new RxBusBean(6654));//通知预览页面刷新数据
        } else {
            Intent intent = new Intent(this, GoodsPreviewActivity.class);
            intent.putExtra("goods_id", goodsId);
            startActivity(intent);
        }
        finish();
    }

    @Override
    protected void afterView(Bundle savedInstanceState) {
        super.afterView(savedInstanceState);
        goodsId = getIntent().getStringExtra("goods_id");//添加的时候传进来
        bean = (GoodsInfoBean) getIntent().getSerializableExtra("data");//修改的时候传进来
        adapter = new SelectImageAdapter(this, 10);
        imageGridView.setAdapter(adapter);

        if (bean != null) {//代表修改
            goodsId = bean.getBatch_goods_id() + "";
            adapter.addStringList(bean.getContent_images());
            editText.setText(bean.getDescription());
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == PhotoPicker.REQUEST_CODE) {
            if (data != null) {
                List<String> images = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                for (String imagePath : images) {
                    adapter.addOneImage(imagePath);
                }
            }
        }
    }

    @OnClick(R.id.toolbar_left_btn)
    public void onClickBack() {
        finish();
    }

    @OnClick(R.id.goods_detail_commit)
    public void onClick() {

        String content = editText.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            showMessage("请输入商品描述");
            return;
        }
        if (content.length() > 300) {
            showMessage("超过最长限制");
            return;
        }
        if (adapter.getStringList().size() <= 0) {
            showMessage("请选择图片");
            return;
        }
        mPresenter.addGoodsThirdStep(adapter.getStringList(), content, goodsId);

    }
}
